package com.xxx.kjds.controller;


import com.xxx.kjds.entity.FinanceCheck;
import com.xxx.kjds.entity.TraceKey;
import com.xxx.kjds.entity.User;
import com.xxx.kjds.entity.Wallets;
import com.xxx.kjds.service.FinanceService;
import com.xxx.kjds.service.TraceService;
import com.xxx.kjds.service.UserService;
import com.xxx.kjds.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/wallet")
public class WalletController {

    @Autowired
    private WalletService walletsService;
    @Autowired
    private UserService userService;
    @Autowired
    private FinanceService financeService;
    @Autowired
    private TraceService traceService;


    /*
     * 根据token获取用户名，根据用户名查找user
     * 根据user的部分信息填充页面，
     * 注册钱包
     * */
    @RequestMapping("/register")
    @ResponseBody
    public String Rigister(HttpServletRequest request) {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        String pw = request.getParameter("pw");

        Wallets wallets = new Wallets();
        wallets.setMoney(0.00);
        wallets.setPassword(pw);
        wallets.setUserId(uid);

        int result = walletsService.insert(wallets);
        if (result > 0) {
            return "true";
        }
        return "false";
    }

    /*
     * 根据uid和npw修改支付密码
     * */
    @RequestMapping("/updatePw")
    @ResponseBody
    public String updatePw(HttpServletRequest request) {
        //获取uid
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        String pw = request.getParameter("pw");

        //根据用户名查找account
        Wallets wallets = walletsService.selectByUid(uid);
        wallets.setPassword(pw);

        int result = walletsService.updateByPrimaryKey(wallets);
        if (result > 0) {
            return "true";
        }
        return "false";
    }

    //需要事务，todo
    //品牌商，取现
    @RequestMapping("/cashout")
    @ResponseBody
    public String cashOut(HttpServletRequest request) {
        //uid
        Integer uid = Integer.valueOf(request.getParameter("uid"));

        //wid
        Wallets wallets = walletsService.selectByUid(uid);
        Integer wid = wallets.getId();

        //余额
        double balance = wallets.getMoney();
        //提现金额
        double money = Double.parseDouble(request.getParameter("money"));

        System.out.println(uid);
        System.out.println(wid);
        System.out.println(money);
        //判断金额是否为负数
        if (money < 0.0) {
            return "请保证提现金额大于0";
        }
        //判断余额是否足够
        if (money > balance) {
            return "您的余额已不足，请减少提现金额";
        }

        //输入的支付密码
        String password = request.getParameter("pw");
        //从数据库读取wid的密码
        String pw = wallets.getPassword();
        //判断支付密码是否正确
        if (!password.equals(pw)) {
            return "支付密码不正确，请重新输入";
        }

        //往finance表插入
        FinanceCheck FinanceCheck = new FinanceCheck();
        FinanceCheck.setType("提现");
        FinanceCheck.setMoney(money);
        //获取系统当前时间
        Date applyTime = new Date();
        FinanceCheck.setApplyTime(applyTime);
        System.out.println(applyTime);
        FinanceCheck.setState("待审核");

        int result = financeService.insert(FinanceCheck);
        if (result == 0) {
            return "finance插入失败";
        }

        //遍历找到最大的fid，作为新增记录到的fid
        List<FinanceCheck> financeCheckList = financeService.getList();
        int max = 0;
        int fid = 0;
        for (com.xxx.kjds.entity.FinanceCheck financeCheck : financeCheckList) {
            if (financeCheck.getId() > max) {
                max = financeCheck.getId();
            }
        }
        fid = max;
        System.out.println(fid);
        TraceKey traceKey = new TraceKey();
        traceKey.setFcId(fid);
        traceKey.setWalletsId(wid);
        int result2 = traceService.insert(traceKey);
        if (result2 == 0) {
            return "trace表插入失败";
        }
        return "true";
    }


    //需要事务，todo
    //借卖方，充值
    @RequestMapping("/cashin")
    @ResponseBody
    public String cashIn(HttpServletRequest request) {
        //uid
        Integer uid = Integer.valueOf(request.getParameter("uid"));

        //wid
        Wallets wallets = walletsService.selectByUid(uid);
        Integer wid = wallets.getId();
        System.out.println(uid);
        System.out.println(wid);
        //余额
        double balance = wallets.getMoney();
        //提现金额
        double money = Double.valueOf(request.getParameter("money"));
        System.out.println(money);
        //判断金额是否为负数
        if (money < 0.0) {
            System.out.println("请保证充值金额大于0");

            return "请保证充值金额大于0";
        }

        //输入的支付密码
        String password = request.getParameter("pw");
        //从数据库读取wid的密码
        String pw = wallets.getPassword();
        //判断支付密码是否正确
        if (!password.equals(pw)) {
            System.out.println("支付密码不正确，请重新输入");
            return "支付密码不正确，请重新输入";
        }

        //往finance表插入
        FinanceCheck FinanceCheck = new FinanceCheck();
        FinanceCheck.setType("充值");
        FinanceCheck.setMoney(money);
        //获取系统当前时间
        Date applyTime = new Date();
        FinanceCheck.setApplyTime(applyTime);
        System.out.println(applyTime);
        FinanceCheck.setState("待审核");

        int result = financeService.insert(FinanceCheck);
        if (result == 0) {
            System.out.println("finance插入失败");
            return "finance插入失败";
        }

        //遍历找到最大的fid，作为新增记录到的fid
        List<FinanceCheck> financeCheckList = financeService.getList();
        int max = 0;
        int fid = 0;
        for (FinanceCheck financeCheck : financeCheckList) {
            if (financeCheck.getId() > max) {
                max = financeCheck.getId();
            }
        }
        fid = max;
        System.out.println(fid);
        TraceKey traceKey = new TraceKey();
        traceKey.setFcId(fid);
        traceKey.setWalletsId(wid);
        int result2 = traceService.insert(traceKey);
        if (result2 == 0) {
            System.out.println("trace表插入失败");
            return "trace表插入失败";
        }
        System.out.println("true");
        return "true";
    }


    @RequestMapping(value = "/selectByUname")
    @ResponseBody
    public Double selectByUname(HttpServletRequest request) {
        String uname = request.getParameter("uName");
        User user = userService.selectByName(uname);
        Integer uid = user.getId();
        Wallets wallets = walletsService.selectByUid(uid);
        return wallets.getMoney();
    }


    @RequestMapping(value = "/selectByUid")
    @ResponseBody
    public Wallets selectByUid(HttpServletRequest request) {
        //查询结果
        List<Object> list = new ArrayList<>();
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        return walletsService.selectByUid(uid);
    }

    @RequestMapping(value = "/selectMyWalletByUid")
    @ResponseBody
    public String selectMyWalletByUid(HttpServletRequest request) {
        //查询结果
        List<Object> list = new ArrayList<>();
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Wallets wallets = walletsService.selectByUid(uid);
        if (wallets != null) {
            return "true";
        }
        return "false";
    }

    @RequestMapping(value = "/selectBalanceByUid")
    @ResponseBody
    public List<String> selectBalanceByUid(HttpServletRequest request) {
        //查询结果
        List<String> list = new ArrayList<>();

        //获取账户名
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        User user = userService.selectByPrimaryKey(uid);
        list.add(user.getName());

        //获取账户余额
        Wallets wallets = walletsService.selectByUid(uid);
        String balance = String.valueOf(wallets.getMoney());
        list.add(balance);
        return list;
    }


    //根据uid查看账户记录
    @RequestMapping(value = "/selectListByUid")
    @ResponseBody
    public List<FinanceCheck> selectListByWid(HttpServletRequest request) {
        //查询结果
        List<FinanceCheck> result = new ArrayList<>();

        //获取wid
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Wallets wallets = walletsService.selectByUid(uid);
        Integer wid = wallets.getId();

        //根据wid获取FinanceCheck
        List<TraceKey> traceList = traceService.selectByWid(wid);
        for (TraceKey traceKey : traceList) {
            Integer fid = traceKey.getFcId();
            FinanceCheck FinanceCheck = financeService.selectByPrimaryKey(fid);
            result.add(FinanceCheck);
        }

        return result;
    }
}
